<template>
	<view class="slide-content">
		<view class="slide-tips">拖动下方滑块完成拼图</view>
		<view class="slider-pintu">
			<image :src="backgroundImage" class="pintu"></image>
			<view class="pintukuai" :style="{ 'transform': 'translate('+ oldx +'px,'+ top +'px)'   }">
				<image :src="sliderImage"></image>
			</view>
		</view>
		<!-- 滑动 -->
		<view class="slider-movearea" @touchstart="startTouchMove" @touchmove="toucheMove" @touchend="endTouchMove">
			<movable-area :animation="true"><movable-view :x="x" :inertia="true" direction="horizontal"
					@change="startMove"></movable-view></movable-area>
			<view class="huadao">拖动左边滑块完成上方拼图</view>
		</view>
	</view>
</template>
<script>
	export default {
		props: {
			backgroundImage: {
				type: String,
				default: ''
			},
			sliderImage: {
				type: String,
				default: ''
			},
			backgroundImageWidth: {
				type: Number,
			},
			backgroundImageHeight: {
				type: Number,
			},
			sliderImageWidth: {
				type: Number,
			},
			sliderImageHeight: {
				type: Number,
			},
		},
		data() {
			return {
				left: 0, //随机拼图的最终X轴距离
				top: 0, //拼图的top距离
				x: 0, //初始距离
				oldx: 0, //移动的距离

				//* 命名一个时钟 */
				startTime: '', // new Date()
				endTime: '',
				startX: '',
				startY: '',

				trackArr: [], // 记录
				movePercent: 0, //  
			}
		},
		watch: {
			// 每次打开重新刷新拼图
			isShow(newValue, oldValue) {
				if (newValue) {
					this.refreshVerify(); //刷新
				}
			}
		},
		methods: {

			toucheMove(e) {
				let {
					clientX,
					clientY
				} = e.changedTouches[0]

				// 触摸滑动
				let _this = this

				let offsetX = clientX - this.startX,
					offsetY = clientY - this.startY

				this.trackArr.push({
					x: offsetX,
					y: offsetY,
					type: "move",
					t: (new Date().getTime() - _this.startTime.getTime())
				});
			},

			startTouchMove(e) {
				let {
					clientX,
					clientY
				} = e.changedTouches[0]

				// console.log(clientX, clientY)

				this.startX = clientX

				this.startY = clientY

				this.startTime = new Date()
			},

			// 获取最终结果
			getResult() {
				this.$emit('result', {})
			},

			/* 滑动前 */
			startMove(e) {
				this.oldx = e.detail.x;
				let _this = this
				this.trackArr[0] =
				{
					x: 0,
					y: 0,
					type: "down",
					t: (new Date().getTime() - _this.startTime.getTime())
				}

			},

			/* 滑动结束 */
			endTouchMove() {
				this.endTime = new Date()
				// console.log(this.endTime - this.startTime);
				var that = this;
				// 触发 此次滑动行为

				this.$emit('result', {
					startSlidingTime: that.startTime,
					endSlidingTime: that.endTime,
					trackList: that.trackArr
				})


				// 返回数据
				// startSlidingTime:
				// endSlidingTime:
				// trackList: {
				// 	x:
				// 	y:
				// 	type: 
				// 	t:
				// }


				// uni.showToast({
				// 	title: '验证成功',
				// 	duration: 2500,
				// 	success() {
				// 		that.$emit('touchSliderResult', true);
				// 	}
				// });

			},

			/* 重置阴影位置 */
			resetMove() {
				this.x = 1;
				this.oldx = 1;
				setTimeout(() => {
					this.x = 0;
					this.oldx = 0;
				}, 300);
			},

			// 关闭
			closeSlider() {
				this.$emit('touchSliderResult', false);
			},

			//刷新验证
			refreshVerify() {

				// var gl = Math.random().toFixed(2);
				// this.left = uni.upx2px(560) * gl > uni.upx2px(280) ? uni.upx2px(280) : uni.upx2px(560) * gl + uni.upx2px(150); //生成随机X轴最终距离
				// this.top = uni.upx2px(190) * gl; //生成随机Y轴初始距离

				// if (gl <= 0.2) {
				// 	this.img = 1;
				// }
				// if (gl > 0.2 && gl <= 0.4) {
				// 	this.img = 2;
				// }
				// if (gl > 0.4 && gl <= 0.6) {
				// 	this.img = 3;
				// }
				// if (gl > 0.6 && gl <= 0.8) {
				// 	this.img = 4;
				// }
				// if (gl > 0.8 && gl <= 1) {
				// 	this.img = 5;
				// }

				this.resetMove(); //重置阴影位置
			},
		}
	}
</script>

<style lang="scss" scoped>
	.slide-content {
		color: #000;
		width: 560rpx;
		margin: 0 auto;

		.slide-tips {
			font-size: 28rpx;
			color: rgba(2, 20, 33, 0.45);
			padding: 0.5em 0;
		}

		.slider-pintu {
			position: relative;
			width: 100%;
			border-radius: 10rpx;
			overflow: hidden;

			.pintu {
				width: 560rpx;
				height: 315rpx;
				display: block;
				margin: 0 auto;
			}

			.pintukuai {

				position: absolute;
				top: -1px;
				left: 0;
				width: 100rpx;
				height: 316rpx;
				z-index: 100;
				overflow: hidden;

				image {
					display: block;
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
				}
			}
		}

		.yinying {
			position: absolute;
			width: 120rpx;
			height: 120rpx;
			background-color: rgba(0, 0, 0, 0.5);
		}

	}



	.slider-movearea {
		position: relative;
		height: 80rpx;
		width: 100%;
		margin: 25rpx auto;

		movable-area {
			height: 80rpx;
			width: 100%;

			movable-view {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				background-image: url(../../static/slider-bth.png);
				background-repeat: no-repeat;
				background-size: auto 60rpx;
				background-position: center;
				position: relative;
				z-index: 100;
			}
		}
	}

	.huadao {
		width: 100%;
		height: 60upx;
		line-height: 60upx;
		background: #eee;
		box-shadow: inset 0 0 5upx #ccc;
		border-radius: 40rpx;
		color: #999;
		text-align: center;
		box-sizing: border-box;
		position: absolute;
		top: 0rpx;
		left: 0;
		font-size: 28rpx;
		z-index: 99;
	}
</style>